// pages/adress/adress.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_list:[]
  },
  deladdress:function(e){
    var ids = e.currentTarget.dataset.ids
    var address_list = this.data.address_list
    var that = this
    for (var i in address_list){
      if (address_list[i].address_id == ids){
        address_list.splice(i,1)
        break
      }
    }
    wx.showModal({
      title: '确定删除此地址吗？',
      success:function(res){
        if (res.confirm){
          app.ajax({
            url: app.host + "mall/user/delAddress",
            data:{
              ids:ids
            },
            success:function(){
              wx.showToast({
                title: '删除成功',
              })
            that.setData({
              address_list: address_list
            })
            }
          })
        }
      }
    })
  },
  edit:function(e){
    var address_id = e.currentTarget.dataset.address_id
    wx.navigateTo({
      url: '/pages/add_adress/add_adress?address_id=' + address_id,
    })
  },
  address:function(e){
    var address_id = e.detail.value
    app.ajax({
      url: app.host + "mall/user/addAddress",
      data:{
        address_id: address_id
      },
      success:function(){
        wx.showToast({
          title: '操作成功',
        })
      }
    })
  },
  choose_default:function(e){
    var address_id = e.currentTarget.dataset.address_id
    var that = this
    if (that.isback) {
      app.ajax({
        url: app.host + "mall/user/addAddress",
        data: {
          address_id: address_id,
          type:1
        },
        success: function () {
          
            wx.showToast({
              title: '操作成功',
            })
            wx.navigateBack()
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isback){
      this.isback = options.isback
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.showLoading({
      title: '加载中'
    })
    app.ajax({
      url: app.host + "mall/user/address",
      success: function (res) {
        wx.hideLoading()
        that.setData({
          address_list: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})